import gameConfig from "../../../engines/configs/gameConfig";
import { modules, services } from "../../../engines/gameEngine";
import { REWARD_TYPE } from "../../../engines/services/userContent/userInterface";
import NormalLevelBase from "../../../Game/Base/LevelBase";
import { PAGE } from "../../../Game/Enum";
import { ITipsPagePrams } from "../../../Game/Interface";
import mainRoot from "../../../Game/MainRoot";
import levelAvoidEnemy from "./base/levelAvoidEnemy";
import { ScheduleCom } from "./base/ScheduleCom";

const { ccclass, property } = cc._decorator;

@ccclass
export default class levelAvoidBase extends NormalLevelBase {
    @property({ type: cc.Node, displayName: "操作目标" })
    player: cc.Node = null;

    @property({ type: cc.Node, displayName: "操作界面" })
    touchnode: cc.Node = null;

    @property({ type: cc.Node, displayName: "移动范围" })
    moveGroup: cc.Node = null;

    @property({ displayName: "移动速度" })
    moveSpeed: number = 2

    @property({ type: cc.Label, displayName: "倒计时lab" })
    lab_time: cc.Label = null;
    @property({ displayName: "倒计时" })
    time: number = 0;
    @property({ type: levelAvoidEnemy, displayName: "穷" })
    node_qiong: levelAvoidEnemy = null;

    com: ScheduleCom = null;
    onLoad() {
        super.onLoad();

        this.touchnode.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.touchnode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.touchnode.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this)
        this.touchnode.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        this.lab_time.string = `${Math.ceil(this.time)}s`

        this.com = new ScheduleCom()
    }
    protected update(dt: number): void {
        if (!this.startGame) return
        if (this.openTips) return
        if (this.isPause && this.playStopMove) return
        this.time -= dt;
        if (this.time <= 0) {
            this.time = 0
            this.startGame = false;
            this.gamePause(true);
            this.Win()
        }
        this.lab_time.string = `${Math.ceil(this.time)}s`

        this.com.update(dt)
    }
    openTips: boolean = false;
    OnTips() {
        cc.audioEngine.pauseAll();
        // super.OnTips()
        this.openTips = true;

        let rewardFun: () => void = null;
        // console.error(this.levelInfo.reward);
        if (this.levelInfo.reward) {
            rewardFun = () => {
                console.warn("!!!没有");
                if (!mainRoot.GotTips) {
                    services().userSrv.setPower(gameConfig.ADD_ONCE_POWER, true, true);
                    modules().uiControlModule.showGetRewarded(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
                }
            };
        }
        let tipsPrams: ITipsPagePrams = {
            mode: this.m_mode,
            tips: this.tips,
            onReward: rewardFun,
            onClose: () => {
                this.openTips = false;
                cc.audioEngine.resumeAll();
            }
        }
        mainRoot.UISystem.OpenPage(PAGE.TIPSPAGE, tipsPrams);
    }
    startGame: boolean = false;
    setGameStart() {
        this.startGame = true
    }
    touchStartPos: cc.Vec2 = null;
    playerStartPos: cc.Vec2 = null;
    touchStart(event: cc.Event.EventTouch) {
        if (!this.startGame) this.setGameStart()
        if (this.isPause && this.playStopMove) return
        this.touchStartPos = this.player.parent.convertToNodeSpaceAR(event.getLocation());
        this.playerStartPos = this.player.getPosition();
    }
    touchMove(event: cc.Event.EventTouch) {
        if (this.isPause && this.playStopMove) return
        let touchpos = this.player.parent.convertToNodeSpaceAR(event.getLocation());
        let pos = this.playerStartPos.add(touchpos.sub(this.touchStartPos).normalize().mul(touchpos.sub(this.touchStartPos).mag() * this.moveSpeed))
        if (Math.abs(pos.x) > ((this.moveGroup.width / 2) - (this.player.width / 2) * this.player.scaleX)) {
            pos.x = (pos.x / Math.abs(pos.x)) * ((this.moveGroup.width / 2) - (this.player.width / 2) * this.player.scaleX)
        }
        if (Math.abs(pos.y) > (this.moveGroup.height / 2) - (this.player.height / 2) * this.player.scaleY) {
            pos.y = (pos.y / Math.abs(pos.y)) * ((this.moveGroup.height / 2) - (this.player.height / 2) * this.player.scaleY)
        }
        this.touchStartPos = touchpos
        this.playerStartPos = pos
        this.player.setPosition(pos)
    }
    touchEnd(event: cc.Event.EventTouch) {
        if (this.isPause && this.playStopMove) return

        this.touchStartPos = null
        this.playerStartPos = null
    }

    isPause: boolean = false;
    playStopMove: boolean = false;
    /**设置游戏暂停 */
    gamePause(bool: boolean) {
        this.isPause = bool;
        this.playStopMove = bool;
    }

    scheduleOn(call: () => void, time: number) {
        // let dttime = 0
        // let func = () => {
        //     if (this.isPause) return
        //     if (this.openTips) return
        //     dttime += cc.director.getDeltaTime()
        //     if (dttime >= time) {
        //         this.unschedule(func)
        //         call()
        //     }
        // }
        // this.schedule(func)

        this.com.add(call, time, 1)
    }

    scheduleAny(call: () => void, time: number = 0) {
        // let dttime = 0
        // let func = () => {
        //     if (this.isPause) return
        //     if (this.openTips) return
        //     dttime += cc.director.getDeltaTime()
        //     if (dttime >= time) {
        //         dttime = 0;
        //         call()
        //     }
        // }
        // this.schedule(func)

        return this.com.add(call, time)
    }

    newLabNode(colorHEX: string = "#e4664f"): levelAvoidEnemy {
        let node = cc.instantiate(this.node_qiong.node)
        node.parent = this.node_qiong.node.parent;
        node.getComponent(levelAvoidEnemy).lab.node.color = new cc.Color().fromHEX(colorHEX);
        return node.getComponent(levelAvoidEnemy)
    }
}